package com.yipei.business.controller;

import com.yipei.business.domain.MlTradeAccountFinancing;
import com.yipei.business.service.IMlTradeAccountFinancingService;
import com.yipei.common.annotation.Log;
import com.yipei.common.core.controller.BaseController;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.core.page.TableDataInfo;
import com.yipei.common.enums.BusinessType;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 账号融资Controller
 *
 * @author yipei
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/business/tradefinancing")
@Api(tags = "账号融资", description = "账号融资")
public class MlTradeAccountFinancingController extends BaseController {
    @Autowired
    private IMlTradeAccountFinancingService mlTradeAccountFinancingService;

    /**
     * 查询账号融资列表
     */
    //@PreAuthorize("@ss.hasPermi('business:tradefinancing:list')")
    @ApiOperation("账号融资获取数据")
    @GetMapping("/list")
    public TableDataInfo list(MlTradeAccountFinancing mlTradeAccountFinancing) {
        startPage();
        List<MlTradeAccountFinancing> list = mlTradeAccountFinancingService.selectMlTradeAccountFinancingList(mlTradeAccountFinancing);
        return getDataTable(list);
    }

    /**
     * 导出账号融资列表
     */
    //@PreAuthorize("@ss.hasPermi('business:tradefinancing:export')")
    @ApiOperation("账号融资导出数据")
    @Log(title = "账号融资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlTradeAccountFinancing mlTradeAccountFinancing) {
        List<MlTradeAccountFinancing> list = mlTradeAccountFinancingService.selectMlTradeAccountFinancingList(mlTradeAccountFinancing);
        ExcelUtil<MlTradeAccountFinancing> util = new ExcelUtil<MlTradeAccountFinancing>(MlTradeAccountFinancing.class);
        util.exportExcel(response, list, "账号融资数据");
    }

    /**
     * 获取账号融资详细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:tradefinancing:query')")
    @ApiOperation("账号融资通过ID获取数据")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mlTradeAccountFinancingService.selectMlTradeAccountFinancingById(id));
    }

    /**
     * 新增账号融资
     */
    //@PreAuthorize("@ss.hasPermi('business:tradefinancing:add')")
    @ApiOperation("账号融资新增数据")
    @Log(title = "账号融资", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MlTradeAccountFinancing mlTradeAccountFinancing) throws Exception {
        return toAjax(mlTradeAccountFinancingService.insertMlTradeAccountFinancing(mlTradeAccountFinancing));
    }

    /**
     * 修改账号融资
     */
    //@PreAuthorize("@ss.hasPermi('business:tradefinancing:edit')")
    @ApiOperation("账号融资修改数据")
    @Log(title = "账号融资", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlTradeAccountFinancing mlTradeAccountFinancing) {
        return toAjax(mlTradeAccountFinancingService.updateMlTradeAccountFinancing(mlTradeAccountFinancing));
    }

    /**
     * 删除账号融资
     */
    //@PreAuthorize("@ss.hasPermi('business:tradefinancing:remove')")
    @ApiOperation("账号融资删除数据")
    @Log(title = "账号融资", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mlTradeAccountFinancingService.deleteMlTradeAccountFinancingByIds(ids));
    }

    @ApiOperation("根据当前账号获取融资总数据")
    @GetMapping("/getAccountFinancing")
    public AjaxResult getAccountFinancing(MlTradeAccountFinancing mlTradeAccountFinancing) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        List<MlTradeAccountFinancing> list = mlTradeAccountFinancingService.lambdaQuery().eq(MlTradeAccountFinancing::getTdAcc, tdAcc).list();
        if (!CollectionUtils.isEmpty(list)){
            BigDecimal zAlimit = list.stream().map(MlTradeAccountFinancing::getAlimit).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal sum = list.stream()
                    .map(MlTradeAccountFinancing::getRate)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal rate = sum.divide(BigDecimal.valueOf(list.size()), 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal zYyEd = list.stream().map(MlTradeAccountFinancing::getYyEd).reduce(BigDecimal.ZERO, BigDecimal::add);
            mlTradeAccountFinancing.setAlimit(zAlimit);
            mlTradeAccountFinancing.setRate(rate);
            mlTradeAccountFinancing.setYyEd(zYyEd);
            mlTradeAccountFinancing.setYh_ll(BigDecimal.ZERO);
            mlTradeAccountFinancing.setZ_qk(BigDecimal.ZERO);
            mlTradeAccountFinancing.setYh_qk(BigDecimal.ZERO);
        }
        return AjaxResult.success(mlTradeAccountFinancing);
    }


    @ApiOperation("根据当前账号获取融资条数")
    @GetMapping("/getAccountFinancingList")
    public AjaxResult getAccountFinancingList(MlTradeAccountFinancing mlTradeAccountFinancing) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        List<MlTradeAccountFinancing> list = mlTradeAccountFinancingService.lambdaQuery().eq(MlTradeAccountFinancing::getTdAcc, tdAcc).orderByDesc(MlTradeAccountFinancing::getCreateTime).list();
        return AjaxResult.success(list);
    }

    /**
     * 还本
     * @param mlTradeAccountFinancing
     * @return
     * @throws Exception
     */
    @ApiOperation("还本")
    @PostMapping("/repayment")
    public AjaxResult repayment(@RequestBody MlTradeAccountFinancing mlTradeAccountFinancing) throws Exception {
        AjaxResult ajaxResult = mlTradeAccountFinancingService.repayment(mlTradeAccountFinancing);
        return ajaxResult;
    }


    /**
     * 还息
     * @param mlTradeAccountFinancing
     * @return
     * @throws Exception
     */
    @ApiOperation("还本")
    @PostMapping("/payInterest")
    public AjaxResult payInterest(@RequestBody MlTradeAccountFinancing mlTradeAccountFinancing) throws Exception {
        AjaxResult ajaxResult = mlTradeAccountFinancingService.payInterest(mlTradeAccountFinancing);
        return ajaxResult;
    }
}
